/*
 * app_bl0942.h
 *
 *  Created on: 2023年11月22日
 *      Author: zlw
 */

#ifndef DS_APP_INS_APP_BL0942_H_
#define DS_APP_INS_APP_BL0942_H_

#include "device_type.h"

#if defined(SM_WS)

#include "bsp_gpio.h"
#include "bsp_config.h"
#include "em_cmu.h"
#include "em_usart.h"
#include "em_eusart.h"
#include "em_gpio.h"
#include "stdio.h"
#include "string.h"
#include "circular_buffer.h"

/************************communicate uart define**************************************/

#define BL0942_COM  EUSART1

#define BL0942_TX_PORT  (gpioPortC)
#define BL0942_TX_PIN   (5)

#define BL0942_RX_PORT  (gpioPortC)
#define BL0942_RX_PIN   (7)

#define BL0942_COM_DEFAULT                                                                      \
  {                                                                                                   \
    eusartEnable,              /* Enable RX/TX when initialization completed. */                      \
    0,                         /* Use current configured reference clock for configuring baud rate.*/ \
    9600,                      /* 115200 bits/s. */                                                   \
    eusartOVS16,               /* Oversampling x16. */                                                \
    eusartDataBits8,           /* 8 data bits. */                                                     \
    eusartNoParity,            /* No parity. */                                                       \
    eusartStopbits1,           /* 1 stop bit. */                                                      \
    eusartMajorityVoteEnable,  /* Majority vote enabled. */                                           \
    eusartLoopbackDisable,     /* Loop back disabled. */                                              \
    NULL,                      /* Default advanced settings. */                                       \
  }

#define BL0942_COM_BUFF_SIZE        (128)

/********************************uart data frame define**************************************************/

#define ELECTRIC_DATA_LEN           (21)

#define FRAME_HEAD  (0x58)
#define READ_CMD    (0xAA)

#define CLEAR_ENERGY_CNT  (0xC7)
#define RESET_MODE_REG    (0x87)

typedef struct{
  uint8_t head;
  uint8_t data[ELECTRIC_DATA_LEN];
  uint8_t checkSum;

  uint8_t     recv_state;
  uint8_t     recv_len;
}uartData_t;

typedef struct{
  uint8_t iRms[3];
  uint8_t vRms[3];
  uint8_t iFastRms[3];
  uint8_t pRms[3];
  uint8_t cfCnt[3];
  uint8_t freq[3];
  uint8_t state[3];
}electricData_t;

typedef enum{
  PKT_STAT_HEAD,
  PKT_DATA,
  PKT_CHECK_SUM
}pktState_t;

/********************************electric data define************************************************/

#define MEASURE_INTERVAL        (32768/2) //500ms
#define ENERGY_SAVE_INTERVAL    (12*60*32768)//12min

#define CURRENT_OVERLOAD    (14*1000)

typedef enum{
  MEASURE_NONE,
  MEASURE_CALBRATION,
  MEASURE_NORMAL
}measure_t;

typedef enum{
  FREQUENCY_50HZ,
  FREQUENCY_60HZ
}voltageFreq_t;

typedef struct{
  uint8_t currentCalibration;       //40
  uint8_t voltageCalibration;       //75
  uint8_t powerCalibration;         //19
}calibration_t;

typedef struct{
  uint8_t vOverCnt:4;
  uint8_t vUnderCnt:4;
  uint8_t vnormalCnt:4;
  uint8_t p110OverCnt:4;
  uint8_t p220OverCnt:4;
  uint8_t iOverCnt:4;
}overCnt_t;

typedef enum{
  currentOverload,
  currentNormal,
  powerOverload,
  powerNormal,
  voltageOverload,
  voltageUnderload,
  voltageNomal,
  energyStateNum,
}energyState_t;

typedef void (*energyStateCallback)(uint16_t);


#define VOLTAGE_MULTI       (1000000)
#define CURRENT_MULTI       (10000)
#define POWER_MULTI         (10000)

#define CALIBRATION_VOLTAGE (220 * VOLTAGE_MULTI)   //v 2929100
#define CALIBRATION_CURRENT (390 * CURRENT_MULTI) //mA  96850
#define CALIBRATION_POWER   (86 * POWER_MULTI)   //w    44300

#define ENERGY_OVERLOAD_CNT     (5)
#define ENERGY_NORMAL_CNT       (5)

void bl0942_usart_listen(void);

void resetCalibration(void);
void energyStateCallbackRegister(energyState_t state, energyStateCallback callback);
void bl0942_init(void);
voltageFreq_t getVoltageFrequency(void);
measure_t measure_state_get(void);
uint16_t currentGet(void);
uint8_t voltageGet(void);
uint16_t powerGet(void);
uint32_t summaEnergyGet(void);

void calibration_finist_callback(void);

#endif

#endif /* DS_APP_INS_APP_BL0942_H_ */
